package org.utem.datacollector;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.os.Environment;

public class Datos extends Thread{
	Context context;
	
	File f;
	FileOutputStream fis;
	boolean bandera;
	String ptoInteres;
	
	private WifiScanSensor wifi;
	private Acelerometro acc;
	private Brujula brujula;
	private GPS gps;
	
	public Datos(WifiScanSensor wifi, Brujula brujula, GPS gps, Acelerometro acc, String ptoInteres){
		this.wifi = wifi;
		this.brujula = brujula;
		this.gps = gps;
		this.acc = acc;
		this.ptoInteres = ptoInteres;
	}
	
	@Override
	public void run(){
		try {
			bandera = true;
			f = new File(Environment.getExternalStorageDirectory().getPath() + "/archivoDatos.csv");
			fis = new FileOutputStream(f);
			while (bandera){
				String valores = ptoInteres + "|" + System.currentTimeMillis() + "|(";
				for (int i = 0; i < wifi.getScanResults().size(); i++){
					valores += "[" + wifi.getScanResults().get(i).BSSID + "," + wifi.getScanResults().get(i).level + "];";
				}
				valores += ")|";
				valores += "(" + acc.getValores()[0] + ";" + acc.getValores()[1] + ";" + acc.getValores()[2] + ")|";
				valores += "(" + brujula.getAzimuth() + ")|";
				valores += "(" + gps.getLatitud() + ";" + gps.getLongitud() + ")\r\n";
				fis.write(valores.getBytes());
				Thread.sleep(30);
			}
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
	
	public void detener(){
		this.bandera = false;
	}
}
